//
//  DingDanViewController.m
//  Experss
//
//  Created by 嗯，大葱 on 17/7/27.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "DingDanViewController.h"

#import "DingDanTopView.h"//筛选栏

#import "DingDanTableViewCell.h"

#import "DingDanDetailViewController.h"//订单详情
#import "PublicLanguageVController.h"//评价

#import "DingDanModle.h"
#import "DingDanShopModle.h"
#import "DingDanOrdermodle.h"
#import "DingDanSkuListMoldle.h"

@interface DingDanViewController ()<DingDanTopViewDelegate,UITableViewDelegate,UITableViewDataSource>{
    
    UINib *_cellNib;
    
    UIImageView *_lineView;
    
    NSInteger titleType;
    
   int _page;
    
    NSString *statusStr;
    
}

/**  */
@property (nonatomic , strong) DingDanTopView *topView;

/**  */
@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *dataArr2;


@property (nonatomic, strong)MJRefreshBackGifFooter *header;



@end

@implementation DingDanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _page = 1;
    statusStr = @"0";
    self.dataArr = [NSMutableArray array];
    self.dataArr2 = [NSMutableArray array];
    
    _cellNib = [UINib nibWithNibName:@"CellClass" bundle:nil];
    [self addNav];
    [self createTopview];
    
    [self createTableView];
    
    [self GetOrderList];
    
}
-(void)addNav{
    self.navigationItem.title = @"订单";
    self.navigationController.navigationBar.translucent = NO;
    _lineView =  [self findlineviw:self.navigationController.navigationBar];
    /*UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"all_fanhui2"] style:(UIBarButtonItemStyleDone) target:self action:@selector(backAction)];
     backItem.tintColor = [UIColor whiteColor];
     self.navigationItem.leftBarButtonItem = backItem;*/
    UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fenlei_xiaoxi"] style:(UIBarButtonItemStyleDone) target:self action:@selector(messageBtn)];
    rightBtnItem.tintColor = [UIColor lightGrayColor];
    self.navigationItem.rightBarButtonItem = rightBtnItem;
    
}
-(void)messageBtn{
    NSLog(@"messageBtn");
}

- (UIImageView *)findlineviw:(UIView *)view

{
    
    if([view isKindOfClass:[UIImageView class]] &&view.bounds.size.height<=1.0){
        
        return (UIImageView *)view;
        
    }for (UIImageView *subview in view.subviews){
        
        UIImageView *lineview = [self findlineviw:subview];
        
        if (lineview){
            
            return lineview;
            
        }
        
    }
    
    return nil;
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _lineView.hidden = YES;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    self.navigationController.navigationBar.translucent = NO;

    [self GetOrderList];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    _lineView.hidden = NO;
    
}


- (void)createTopview{
    self.topView = [DingDanTopView initWithDingDanTopView];
    
    self.topView.index = 0;
    self.topView.delegate = self;
    
    [self.view addSubview:self.topView];
}

- (void)dingdanTopSelectWithIndex:(NSInteger)index{
    
    // 根据index更新tableView
    
    titleType = index;
    if (index == 0) {//全部
        _page = 1;
        statusStr = @"0";
        [self GetOrderList];
        
        
        
    }else if (index == 1){//代付款
        _page = 1;
        statusStr = @"1";
        [self GetOrderList];
        
    }else if (index == 2){//代发货
        _page = 1;
        statusStr = @"2";
        [self GetOrderList];
        
    }else if (index == 3){//带收获
        _page = 1;
        statusStr = @"3";
        [self GetOrderList];
        
    }
    
    else if (index == 4){//待评价
        
        _page = 1;
        statusStr = @"4";
        [self GetOrderList];
        
    }else if (index == 5){//待退货
        statusStr = @"10";
        _page = 1;
        [self GetOrderList];
    }
    

    
}

- (void)createTableView{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, kScreen_Width, kScreen_Height-104-49) style:(UITableViewStyleGrouped)];
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.delegate = self;
    tableView.dataSource = self;
   
    tableView.bounces = YES;
    tableView.separatorStyle = kSpNone;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    [tableView registerClass:[DingDanTableViewCell class] forCellReuseIdentifier:@"cell"];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    
    self.tableView = tableView;
    
    
    
    
    MJRefreshGifHeader *headNew = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestRefresh)];
    NSMutableArray *refreshingImages2 = [NSMutableArray array];
    for (NSUInteger i =1; i<=12; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",i]];
        [refreshingImages2 addObject:image];
    }
    
    NSMutableArray *idleImages2 = [NSMutableArray array];
    for (NSUInteger i =1; i<=12; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",i]];
        [idleImages2 addObject:image];
    }
    // 设置普通状态的动画图片
    [headNew setImages:idleImages2 forState:MJRefreshStateIdle];
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [headNew setImages:refreshingImages2 forState:MJRefreshStatePulling];
    // 设置正在刷新状态的动画图片
    [headNew setImages:refreshingImages2 duration:2 forState:MJRefreshStateRefreshing];
    // 隐藏状态
    headNew.stateLabel.hidden = YES;
    headNew.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.mj_header = headNew;
    
    
    
    
    _header = [MJRefreshBackGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestDta2)];
    NSMutableArray *refreshingImages = [NSMutableArray array];
    for (NSUInteger i =1; i<=12; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",i]];
        [refreshingImages addObject:image];
    }
    
    NSMutableArray *idleImages = [NSMutableArray array];
    for (NSUInteger i =1; i<=12; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",i]];
        [idleImages addObject:image];
    }
    // 设置普通状态的动画图片
    [_header setImages:idleImages forState:MJRefreshStateIdle];
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [_header setImages:refreshingImages forState:MJRefreshStatePulling];
    // 设置正在刷新状态的动画图片
    [_header setImages:refreshingImages duration:2 forState:MJRefreshStateRefreshing];
    // 隐藏状态
    _header.stateLabel.hidden = YES;
    self.tableView.mj_footer = _header;
    
    
    
    
    
}


#pragma mark －＝－＝－＝－＝－－＝－＝＝＝－＝订单刷新与加载
- (void)requestRefresh {
    _page = 1;
    [self GetOrderList];
}
- (void)requestDta2 {
    _page++;
    [self GetOrderList];
   
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  
    DingDanModle *model = self.dataArr[indexPath.section];
    if ([model.order_data[@"status"] isEqualToString:@"1"] ||[model.order_data[@"status"] isEqualToString:@"2"]||[model.order_data[@"status"] isEqualToString:@"3"]||[model.order_data[@"status"] isEqualToString:@"4"]||[model.order_data[@"status"] isEqualToString:@"8"]||[model.order_data[@"status"] isEqualToString:@"9"]) {
         return  47 + (kScreen_Width - 40 - 30 - 40)/5.0 + 26+6 + 40 + 55;
       
    }else {
        return 47 + (kScreen_Width - 40 - 30 - 40)/5.0 + 26+6 + 40;
    }
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DingDanTableViewCell *cell = [[DingDanTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
     
    DingDanModle *modle = self.dataArr[indexPath.section];
    
    cell.Modle = modle;
        
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 10)];
    view.backgroundColor = KShouYeBg;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DingDanDetailViewController *vc = [[DingDanDetailViewController alloc] init];
    DingDanModle *modle = self.dataArr[indexPath.section];
    DingDanOrdermodle *orderModle = [DingDanOrdermodle mj_objectWithKeyValues:modle.order_data];
    vc.order_id = orderModle.Id;
    vc.navigationController.hidesBottomBarWhenPushed = YES;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleDefault;
}




#pragma mark 
//http://bs.m0571.com/mobileapi/public/baishi/?service=Shop_OrderList.GetOrderList&userid=11&actoken=57cef4171225c88fdea33bfc62d34060.900.1504691184&limit=20&page=1&status=1

-(void)GetOrderList{
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]],@"status":statusStr,@"page":[NSString stringWithFormat:@"%d",_page],@"limit":@"20"};
    [HttpRequest postWithURLString:POST(Shop_GetOrderList) parameters:parameDic success:^(id responseObject) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (_page == 1) {
            [_dataArr removeAllObjects];
        }
        
        if ([[responseObject objectForKey:@"ret"] integerValue] == 200) {
           
            NSArray *order_listArr = [[responseObject objectForKey:@"data"]objectForKey:@"order_list"];
            self.dataArr2 =[DingDanModle mj_objectArrayWithKeyValuesArray:order_listArr];
            
            
            [_dataArr addObjectsFromArray:_dataArr2.mutableCopy];
            
            [self.tableView reloadData];
            if (self.dataArr.count == 0) {
                self.tableView.hidden = YES;
                if (_imageV) {
                    [_imageV removeFromSuperview];
                }
                if (_twoLabel) {
                    [_twoLabel removeFromSuperview];
                }
              
                
                _imageV = [[UIImageView alloc] init];
                _imageV.frame = CGRectMake(kScreen_Width/2-75, 200, 150, 150);
                _imageV.image = [UIImage imageNamed:@"zanwudizhi"];
                [self.view addSubview:_imageV];
                [self.tableView.mj_footer endRefreshing];
                _twoLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreen_Width/2-100, CGRectGetMaxY(_imageV.frame)+20, 200, 30)];
                _twoLabel.text = @"您还没有订单哦，快去逛逛吧";
                _twoLabel.textColor = [UIColor grayColor];
                _twoLabel.font = kFont14;
                _twoLabel.textAlignment = NSTextAlignmentCenter;
                [self.view addSubview:_twoLabel];
        
            }
            else {
                self.tableView.hidden = NO;
                [_imageV removeFromSuperview];
                [_twoLabel removeFromSuperview];
           
            }
            

            
            
        }else{
            [MBProgressHUD showText:[responseObject objectForKey:@"msg"]];
        }
        
    } failure:^(NSError *error) {
        
    }];

}





@end
